﻿#include "plcworker.h"
#include <QDebug>
#include <QThread>

PlcWorker::PlcWorker(QObject *parent) : QObject(parent)
{
	m_bPlcConnect = false;
	m_serialPort = new QSerialPort();//实例化串口类一个对象
	if (m_serialPort->isOpen())//如果串口已经打开了 先给他关闭了
	{
		m_serialPort->clear();
		m_serialPort->close();
	}

	m_serialPort->setPortName("COM2");
	if (!m_serialPort->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
	{
		qDebug() << "open plc error!";
		return;
	}
	else{
		// 连接信号槽 当下位机发送数据QSerialPortInfo 会发送个 readyRead 信号, 我们定义个槽void receiveInfo()解析数据
		connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(receiveInfo()));
		m_serialPort->setBaudRate(QSerialPort::Baud19200, QSerialPort::AllDirections);//设置波特率和读写方向
		m_serialPort->setDataBits(QSerialPort::Data8);		//数据位为8位
		m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
		m_serialPort->setParity(QSerialPort::EvenParity);	//偶校验位
		m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位
		start();
	}


}

PlcWorker::~PlcWorker()
{

}

void PlcWorker::Initialize()
{

}

void PlcWorker::start()
{
	// PLC 需要进行一次开始指令
	QByteArray sPlcHeart = "CR";
	sendInfo(sPlcHeart);
}


void PlcWorker::sendInfo(QByteArray sendBuf)
{
	QByteArray endBuf = "\r";
	sendBuf = sendBuf + endBuf;
	qDebug() << sendBuf;
	m_serialPort->write(sendBuf);////这句是真正的给单片机发数据 用到的是QIODevice::write 具体可以看文档
}

void PlcWorker::receiveInfo()
{
	QByteArray info = m_serialPort->readAll();
	qDebug() << "receiveInfo:" << info;
	if (info == "CC\r\n"){
		m_bPlcConnect = true;
		return;
	}
	if (info == "E1\r\n"){
		m_bPlcConnect = false;
		return;
	}
	if (info == "OK\r\n"){
		return;
	}

	std::string str = info.toStdString();
	int pos = str.find("\r");
	str = str.substr(0, pos);
	int n = atoi(str.c_str());

	switch (m_nSig){
	case ENCODER:
		m_nENCODER = n;
		break;
	case MOTOR1_REV:
		m_nMOTOR1_REV = n;
	default:
		break;
	}
}

void PlcWorker::readPlcInfo(SIG_PLC nSig)
{
	m_nSig = nSig;
	if (!m_bPlcConnect){
		return;
	}
	QByteArray start = "RD ";
	QByteArray sendBuf;
	switch (nSig)
	{
	case MOTOR1_REV:sendBuf = "DM202";
		break;
	case ENCODER:sendBuf = "DM0";
		break;
	default:
		break;
	}
	sendBuf = start + sendBuf;
	sendInfo(sendBuf);
}

void PlcWorker::writePlcInfo(SIG_PLC nSig, int nIndex)
{
	m_nSig = 0;
	if (!m_bPlcConnect){
		return;
	}
	QByteArray start = "WR ";
	QByteArray sK = " ";
	QByteArray index = QString::number(nIndex).toUtf8();
	QByteArray sendBuf;

	switch (nSig)
	{
	case START:
		sendBuf = "MR100";
		break;
	case STOP:
		sendBuf = "MR101";
		break;
	case MOTOR1:sendBuf = "MR103";
		break;
	case MOTOR2:
		break;
	case INCODERA:
		break;
	case INCODERB:
		break;
	case INSENSOR:
		break;
	case INPRESSSW1:
		break;
	case INPAUSESW:
		break;
	case QPULSE1:sendBuf = "MR302";
		break;
	case QDIRECTION1:sendBuf = "MR303";
		break;
	case QPULSE2:
		break;
	case QDIRECTION2:
		break;
	case QENABLED1:sendBuf = "MR305";
		break;
	case QENABLED2:
		break;
	case QGRAP1:sendBuf = "MR306";
		break;
	case QGRAP2:sendBuf = "MR307";
		break;
	case QGRAP3:sendBuf = "MR308";
		break;
	case QGRAP4:sendBuf = "MR309";
		break;
	case QGRAP5:
		break;
	case QGRAP6:
		break;
	case QNG1OUTAIR:sendBuf = "MR312";
		break;
	case QNG2OUTAIR:sendBuf = "MR313";
		break;
	case QNG3OUTAIR:sendBuf = "MR314";
		break;
	case QNG4OUTAIR:sendBuf = "MR315";
		break;
	case QNG5OUTAIR:sendBuf = "MR400";
		break;
	case QNG6OUTAIR:sendBuf = "MR401";
		break;
	case QPLATE:sendBuf = "MR402";
		break;
	case QINAIR:sendBuf = "MR403";
		break;
	case QLINE:sendBuf = "MR404";
		break;
	case QTIMEOUT_LINE:sendBuf = "MR405";
		break;
	case QLIGHTG:sendBuf = "MR408";
		break;
	case QLIGHTY:sendBuf = "MR407";
		break;
	case QLIGHTR:sendBuf = "MR406";
		break;
	case QLIGHTVOX:sendBuf = "MR409";
		break;
	case MOTOR1_REV:sendBuf = "DM202";
		break;
	case MOTOR2_REV:sendBuf = "DM212";
		break;
	case MOTOR_RATION1:
		break;
	case MOTOR_RATION2:
		break;
	case OPTICAL:
		break;
	case PCCD_1:sendBuf = "DM100";
		break;
	case PCCD_2:sendBuf = "DM102";
		break;
	case PCCD_3:sendBuf = "DM104";
		break;
	case PCCD_4:sendBuf = "DM106";
		break;
	case PCCD_5:sendBuf = "DM108";
		break;
	case PCCD_6:sendBuf = "DM110";
		break;
	case MOTOR_NG1:sendBuf = "DM130";
		break;
	case MOTOR_NG2:sendBuf = "DM132";
		break;
	case MOTOR_NG3:sendBuf = "DM134";
		break;
	case MOTOR_NG4:sendBuf = "DM136";
		break;
	case MOTOR_NG5:sendBuf = "DM138";
		break;
	case MOTOR_NG6:sendBuf = "DM140";
		break;
	case RESULT_NUMBER:sendBuf = "DM96";
		break;
	case CAMERA_RESULT:sendBuf = "DM98";
		break;
	case WARN_TIME:sendBuf = "DM230";
		break;
	case STOP_TIME:sendBuf = "DM234";
		break;
	default:
		break;
	}
	sendBuf = start + sendBuf + sK + index;
	sendInfo(sendBuf);
}


void PlcWorker::writePlcInfo(SIG_PLC nSig, float nIndex)
{
	int nIndex1 = 0;
	int nIndex2 = 0;
	splitFloat(nIndex, nIndex1, nIndex2);

	QByteArray start = "WR ";
	QByteArray sK = " ";

	QByteArray index1 = QString::number(nIndex1).toUtf8();
	QByteArray index2 = QString::number(nIndex2).toUtf8();
	QByteArray sendBuf1;
	QByteArray sendBuf2;
	switch (nSig)
	{
	default:
		break;
	}
	sendBuf1 = start + sendBuf1 + sK + index1;
	sendBuf2 = start + sendBuf2 + sK + index2;
	sendInfo(sendBuf1);
	sendInfo(sendBuf2);
}

void PlcWorker::splitFloat(float fIndex, int& nIndex1, int& nIndex2){
	float fData;
	fData = fIndex;
	nIndex1 = (int)fIndex;
	nIndex2 = (int)((fData - nIndex1) * 10);
}
